<?php
include("../../tools/session.php");
include("../../tools/controls.php");
include("../../conf.php");
include("../../tools/pub.php");
include("../../tools/mysql.php");
db_connect();

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

$status = $_GET["status"];
$files = array(
	"1" => "data_power.js",
	"2" => "data_company_position.js"
);
$flag = false;

if ($status == "1")
{
	$filepaths = $conf["filepaths"];
	$filename = $files[$status];
	$datas = array("0: [0, '普通权限', '普通权限', 0]");

	$sql = "SELECT * FROM power WHERE deleted=0 ORDER BY power_id ASC";
	$re = db_query($sql);
	
	foreach ($re as $v)
	{
		$datas[] = $v["value"] . ": [" . $v["power_id"] . ", '" . $v["name"]
		  . "', '" . $v["name_zh"] . "', " . $v["value"] . "]";
	}

	$str = "var __powerArray__ = {\n";
	$str .= implode(",\n", $datas);
	$str .= "}\n";
	
	$handle = fopen($filepaths["oa_data_js"] . $filename, "w");
	if (!fwrite($handle, $str)) { $flag = false; } else { $flag = true; }
	fclose($handle);
}
// 如果 $status 为 2，运行生成“公司－部门－职位”JS 代码
elseif ($status == "2")
{
	$filepaths = $conf["filepaths"];
	$company = array();
	
	/*
	$sql = "SELECT a.company_id company_id, a.zh_name company_name, a.power company_power,
				b.section_id section_id, b.zh_name section_name, b.master_id master_id, b.power section_power, 
				c.position_id position_id, c.zh_name position_name, c.power position_power
			FROM company a, section b, position c
			WHERE a.company_id = b.company_id AND b.section_id = c.section_id
					 AND a.company_id<>0 AND b.section_id<>0 AND c.position_id<>0
			ORDER BY company_id, section_id, position_id ASC";
	*/
	$sql = "SELECT a.company_id company_id, a.zh_name company_name, a.power company_power,
				b.section_id section_id, b.zh_name section_name, b.master_id master_id, b.power section_power, 
				c.position_id position_id, c.zh_name position_name, c.power position_power
			FROM company a LEFT JOIN section b ON a.company_id = b.company_id
					LEFT JOIN position c ON b.section_id = c.section_id
			WHERE a.company_id<>0
			ORDER BY company_id, section_id, position_id ASC";
	$re = db_query($sql);

	// Produce datas into PHP multiple level Array
	foreach ($re as $v)
	{
		$c_id			= $v["company_id"];
		$c_name		= $v["company_name"];
		$c_power		= $v["company_power"];
		$s_id			= $v["section_id"];
		$s_name		= $v["section_name"];
		$s_power		= $v["section_power"];
		$s_masterid	= $v["master_id"];
		$p_id			= $v["position_id"];
		$p_name		= $v["position_name"];
		$p_power		= $v["position_power"];
		
		$c_temp = array(array(), $c_name, $c_power);
		$s_temp = array(array(), $s_name, $s_power, $s_masterid);
		$p_temp = array($p_name, $p_power);
		
		if (!isset($company[$c_id])) { $company[$c_id] = $c_temp; }
		if (!isset($company[$c_id][0][$s_id])) { $company[$c_id][0][$s_id] = $s_temp; }
		if (!isset($company[$c_id][0][$s_id][0][$p_id])) { $company[$c_id][0][$s_id][0][$p_id] = $p_temp; }
	}
	
	// Convert PHP Array into javascript code
		$hash_1 = array();
	foreach ($company as $k1 => $v1)
	{
		$str_1 = "'" . $k1 . "':[{";
		$hash_2 = array();
		foreach ($v1[0] as $k2 => $v2)
		{
			$str_2 = "'" . $k2 . "':[{";
			$hash_3 = array();
			foreach ($v2[0] as $k3 => $v3)
			{
				$str_3 = "'" . $k3 . "':['" . $v3[0] . "','" . $v3[1] . "']";
				$hash_3[] = $str_3;
			}
			$str_2 .= implode(",", $hash_3) . "},'" . $v2[1] . "','" . $v2[2] . "','" . $v2[3] . "']";
			$hash_2[] = $str_2;
		}
		$str_1 .= implode(",", $hash_2) . "},'" . $v1[1] . "','" . $v1[2] . "']";
		$hash_1[] = $str_1;
	}
	$last_str = "var __company_2_position__ = {" . implode(",", $hash_1) . "};";
	
	// Write string to file
	$filename = $files[$status];
	$handle = fopen($filepaths["oa_data_js"] . $filename, "w");
	
	if (!fwrite($handle, $last_str)) { $flag = false; } else { $flag = true; }
	fclose($handle);
}

// 输出
echo $flag ? "Success: 生成 $filename 成功！" : "Error: 生成 $filename 失败！";
?>
